
	const btn = document.getElementById('btn');
	const username = document.getElementById('username');
	const email = document.getElementById('email');
	const password = document.getElementById('password');
	const password2 = document.getElementById('password2');
	var isChecked = true;


	//显示错误信息
	function showError(input, message) {
		//console.log("qqqqq")
		const formControl = input.parentElement;
		formControl.className = 'form-control error';
		//利用input的父节点寻找small标签
		const small = formControl.querySelector('small');
		small.innerText = message;
		isChecked = false;
	}

	function showSuccess(input) {
		const formControl = input.parentElement;
		formControl.className = 'form-control success';
		//className写成"cat dog",css里选择器就得写".cat.dog"
		//console.log("qqqqq")
		isChecked = true;
	}

	function checkEmail(input) {
		const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		if (re.test(input.value.trim())) {
			showSuccess(input);
		} else {
			showError(input, 'Email is not valid');
		}
	}

	//检查填没填
	function checkRequired(input) {
		let isRequired = true;
		if (input.value.trim() === '') {
			showError(input, `${getFieldName(input)} is required`);
			isRequired = false;
		} else {
			showSuccess(input);
		}
		return isRequired;
	}

	function checkLength(input, min, max) {
		if (input.value.length < min) {
			showError(input, `${getFieldName(input)} must be at least ${min} characters`);
		} else if (input.value.length > max) {
			showError(input, `${getFieldName(input)} must be no more than ${max} characters`);
		} else {
			showSuccess(input);
		}
	}

	function checkPasswordsMatch(input1, input2) {
		if (input1.value !== input2.value) {
			showError(input2, 'Passwords do not match');
		} else {
			showSuccess(input2);
		}
	}

	function getFieldName(input) {
		return input.id.charAt(0).toUpperCase() + input.id.slice(1);
		//首字母大写
	}

//	不知道为什么不能用addEventListener，没法post

btn.onclick = function () {
	if (checkRequired(username)) {
			checkLength(username, 3, 15);
		}
		if (checkRequired(password)) {
			checkLength(password, 6, 25);
			checkPasswordsMatch(password, password2);
		}
		if (checkRequired(email)) {
			checkEmail(email);
	}

	//console.log(validate());

		
}

//这里有个bug。比如说我密码长度有问题，ischecked变成false
//但是邮箱正确，由于邮箱实在后边检查的，ischecked又变成true了
//导致validate返回的是true，依然会传到数据库

function validate() {
	if (!isChecked)
		return false;
	alert('注册成功');
	//alert弹出的框必须得按确认才能继续加载
	return true;
}


//export { isChecked };



